﻿// This JavaScript program is copyright by Jasper Bedaux (C) 2003.
// THIS SCRIPT COMES WITHOUT ANY WARRANTY OF ANY KIND.
// 
// A link to http://bedaux.net/cpp2html/ will be appreciated.

versionOK = true

if (versionOK) {
	keys = new Array() // containing C++ language elements

	keys.push({style:"comment", start:/\s*\/\*[\s\S]*?\*\//mg})
	keys.push({style:"comment", start:/\s*\/\//mg, end:/\n/mg, neglect:/\\|\?\?\//mg})
	keys.push({style:"pre", start:/\s*?^\s*(?:#|\?\?=|%:)/mg, end:/\n/m, neglect:/\\[\s\S]|\?\?\/[\s\S]/m})
	keys.push({style:"string", start:/\s*(?:\bL)?"/mg, end:/"/m, neglect:/\\[\s\S]|\?\?\/[\s\S]/m})
	keys.push({style:"char", start:/\s*(?:\bL)?'/mg, end:/'/m, neglect:/\\[\s\S]|\?\?\/[\s\S]/m})
	keys.push({style:"float", start:/\s*(?:(?:\b\d+\.\d*|\.\d+)(?:E[\+\-]?\d+)?|\b\d+E[\+\-]?\d+)[FL]?\b|\s*\b\d+\./mgi})
	keys.push({style:"int", start:/\s*\b(?:0[0-7]*|[1-9]\d*|0x[\dA-F]+)(?:UL?|LU?)?\b/mgi})
	keys.push({style:"bool", start:/\s*\b(?:true|false)\b/mg})
	keys.push({style:"type", start:/\s*\b(?:bool|char|double|float|int|long|short|signed|unsigned|void|wchar_t)\b/mg})
	keys.push({style:"flow", start:/\s*\b(?:break|case|catch|continue|default|do|else|for|goto|if|return|switch|throw|try|while)\b/mg})
	keys.push({style:"keyword", start:/\s*\b(?:asm|auto|class|const_cast|const|delete|dynamic_cast|enum|explicit|export|extern|friend|inline|main|mutable|namespace|new|operator|private|protected|public|register|reinterpret_cast|sizeof|static_cast|static|struct|template|this|typedef|typeid|typename|union|using|virtual|volatile)\b/mg})
	keys.push({style:"operator", start:/\s*[\{\}\[\]\(\)<>%:;\.\?\*\+\-\^&\|~!=,\\]+|\s*\//mg})
	keys.push({style:"operator", start:/\s*\b(?:and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|xor|xor_eq)\b/mg})


	function toHTML(s) { // convert special chars
		s = s.split("&").join("&amp;");
		s = s.split("<").join("&lt;");
		return s.split(">").join("&gt;");
	}
	function beforeKey(key) {	// addition
		return "<span class=\"" + key.style + "\">";
	}
	function afterKey(key){
		return "</span>";
	}
}
else alert("Your browser does not seem to support Javascript version 1.5 or higher.\n")

function colorCode(code) {
	if (versionOK) {
		var s = code // get code

		s = s.replace(/\r\n?/gm, "\n") // convert DOS and/or MAC to UNIX
		var newCode = "";
		var keyString = ""
		var match = 0

		var previousMatch = -1
		for (var i = 0; i != keys.length; ++i) keys[i].startPos = -1
		for (var position = 0; position != s.length; position = keys[match].endPos) {
			for (var i = 0; i != keys.length; ++i) {
				if (keys[i].startPos < position) { // update needed
					keys[i].start.lastIndex = position
					var result = keys[i].start.exec(s)
					if (result != null) {
						keys[i].startPos = result.index
						keys[i].endPos = keys[i].start.lastIndex
					}
					else keys[i].startPos = keys[i].endPos = s.length
				}
			}
			match = 0
			for (var i = 1; i < keys.length; ++i) // find first matching key
				if (keys[i].startPos < keys[match].startPos) match = i
			if (keys[match].end != undefined) { // end must be found
				var end = new RegExp(keys[match].end.source + "|" + keys[match].neglect.source, "mg")
				end.lastIndex = keys[match].endPos
				while (keys[match].endPos != s.length) {
					result = end.exec(s)
					if (result != null) {
						if (result[0].search(keys[match].end) == 0) {
							keys[match].endPos = end.lastIndex
							break
						}
					}
					else keys[match].endPos = s.length
				}
			}
			var before = s.substring(position, keys[match].startPos)
			keyString = s.substring(keys[match].startPos, keys[match].endPos)
			var output = ""
			if ((before == "") && (match == previousMatch))
				output += toHTML(keyString)
			else {
				if (previousMatch != -1) output += afterKey(keys[previousMatch]) 
				output += toHTML(before)
				if (keyString != "") output += beforeKey(keys[match]) + toHTML(keyString)
			}
			previousMatch = match
			newCode += output
		}
		if (keyString != "") 
		newCode += afterKey(keys[match])
		return "<pre>" + newCode + "</pre>";
	}
	else alert("Your browser does not seem to support Javascript version 1.5 or higher.\n")
}
